package cn.itsource.hrm.controller;

import cn.itsource.hrm.controller.vo.CourseVo;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.service.ICourseTypeService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;
    @Autowired
    private ICourseTypeService courseTypeService;

    /**
    * 保存和修改公用的
    * @param courseVo  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseVo courseVo){
        System.out.println(courseVo.getName());
        try {
            if(courseVo.getId()!=null){
                courseService.updateById(courseVo);
            }else{
                courseService.save(courseVo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            courseService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
    * 获取对象信息
    * @return
    */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Course get(@PathVariable("id")Long id)
    {

        return courseService.getById(id);
    }


    /**
    * 查看所有的对象信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Course> list(){

        return courseService.list(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Course> json(@RequestBody CourseQuery query)
    {
        return courseService.queryPage(query);
    }

    /**
     * 上线课程
     * @return
     */
    @PostMapping("/online")
    public AjaxResult online(@RequestBody List<Long> ids){
        try {
            courseService.online(ids);
            return AjaxResult.me().setSuccess(true).setMessage("上线成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(true).setMessage("上线失败"+e.getMessage());
        }
    }
    /**
     * 下线课程
     * @return
     */
    @PostMapping("/offline")
    public AjaxResult offline(@RequestBody List<Long> ids){
        try {
            courseService.offline(ids);
            return AjaxResult.me().setSuccess(true).setMessage("下线成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(true).setMessage("下线失败"+e.getMessage());
        }
    }

    /**
     * 课程首页静态化
     * @return
     */
    @GetMapping("/staticHomePage")
    public AjaxResult staticHomePage (){
        try {
            courseTypeService.staticHomePage();
            return AjaxResult.me().setSuccess(true).setMessage("课程首页静态化成功");

        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("课程首页静态化失败"+e.getMessage());
        }

    }
}
